class weixin{
    private $times;				//消息发送时间
	private $tokey="";    		//tokey
	/*------------------*/
	private $fromUsername;		//发给的用户		
	private $toUsername;		//自己
	private $totime;			//消息时间
	private $type;				//消息类型
	
	//接收tokey
	function __construct($tokey){	
		$this->tokey=$tokey; 
	}
	
	//微信链接调用
	public function valid(){
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }
	
	//微信验证消息真实性
	private function checkSignature(){
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
		$token = $this->tokey;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
	
	public function responseMsg(){	
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		if(!empty($postStr)){
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			$this->fromUsername = $postObj->FromUserName;
			$this->toUsername   = $postObj->ToUserName;
			$this->totime  = $postObj->CreateTime;
			$this->type    = $postObj->MsgType;
			
			$data=array("touser"=>$this->toUsername,
						"fromuser"=>$this->fromUsername,
						"totime"=>$this->totime,
						"type"=>$this->type
						);
						
			switch($this->type){
				//获取文本消息
				case "text" :
					$data['key']=trim($postObj->Content);
					return $data;
					break;
				//获取图片消息
				case "image":
					$data['picurl']=$postObj->PicUrl;
					return $data;
					break;
				//获取语音推送
				case "voice":
					$data['mediaId']=$postObj->MediaId;
					$data['format']=$postObj->Format;
					return $data;
					break;
				//获取视频推送
				case "video":
					$data['mediaId']=$postObj->MediaId;
					$data['thumbMediaId']=$postObj->ThumbMediaId;
					return $data;
					break;
				//获取地理位置消息
				case "location":
					$data['x']=$postObj->Location_X;
					$data['y']=$postObj->Location_Y;
					$data['scale']=$postObj->Scale;
					$data['label']=$postObj->Label;
					return $data;
					break;
				//获取链接消息
				case "link":
					$data['title']=$postObj->Title;
					$data['descr']=$postObj->Description;
					$data['url']=$postObj->Url;
					return $data;
					break;
				//获取事件推送
				case "event":
					$data['event']=$postObj->Event;
					switch($postObj->Event){
						//获取2微码关注消息
						case "subscribe":
							$data['eventKey']=$postObj->EventKey;
							$data['ticket']=$postObj->Ticket;
							return $data;
							break;
						//获取2微码参数消息
						case "SCAN":
							$data['eventKey']=$postObj->EventKey;
							$data['ticket']=$postObj->Ticket;
							return $data;
							break;
						//上报地理位置
						case "LOCATION":
							$data['x']=$postObj->Latitude;
							$data['y']=$postObj->Longitude;
							$data['Precision']=$postObj->Precision;
							return $data;
							break;	
						//自定义菜单
						case "CLICK":
							$data['eventKey']=$postObj->EventKey;
							return $data;
							break;							
					}
					break;
			}
		}else{
			$this->reply("text","出错了！请联系开发者.");
			exit;
		}
	}
	//微信封装类,
	//type: text 文本类型, news 图文类型
	//text,array(内容),array(ID)
	//news,array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
	function reply($type,$value_arr,$o_arr=array(0)){
		$this->times = time();
		$con="<xml>
		<ToUserName><![CDATA[{$this->fromUsername}]]></ToUserName>
		<FromUserName><![CDATA[{$this->toUsername}]]></FromUserName>
		<CreateTime>{$this->times}</CreateTime>
		<MsgType><![CDATA[{$type}]]></MsgType>";
		 switch($type){
			//文本回复
			case "text" : 
				$con.="<Content><![CDATA[{$value_arr}]]></Content>
				<FuncFlag>{$o_arr}</FuncFlag>";  
				break;
			//音乐消息
			case "music" :
				$con.="<Music>
				 <Title><![CDATA[".$value_arr['title']."]]></Title>
				 <Description><![CDATA[".$value_arr['description']."]]></Description>
				 <MusicUrl><![CDATA[".$value_arr['musicurl']."]]></MusicUrl>
				 <HQMusicUrl><![CDATA[".$value_arr['hqmusicurl']."]]></HQMusicUrl>
				</Music>";
				break;
			//图文回复
			case "news" : 
				$con.="<ArticleCount>{$o_arr[0]}</ArticleCount>
				<Articles>";
				foreach($value_arr as $id=>$v){
				if($id>=$o_arr[0]) break; else null; 	//判断数组数不超过设置数
					$con.="<item>
					<Title><![CDATA[{$v[0]}]]></Title> 
					<Description><![CDATA[{$v[1]}]]></Description>
					<PicUrl><![CDATA[{$v[2]}]]></PicUrl>
					<Url><![CDATA[{$v[3]}]]></Url>
					</item>";
				}
				$con.="</Articles>
				<FuncFlag>{$o_arr[1]}</FuncFlag>";  
				break;
		 }
		echo $con."</xml>";
	}
?>